// 格式化时间
export const formatTime = (time) => time.replace(/T|Z/g, ' ').replace(/(-)/g, '/')

// 判断是否为web环境
export const isWeb = () => typeof window !== 'undefined'

// 判断是否为移动端
export const isMobile = () => isWeb() && /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)

// 判断是否为微信浏览器
export const isWeixin = () => isWeb() && /MicroMessenger/i.test(navigator.userAgent)

// 聚焦元素
export const placeCaretAtEnd = (node) =>{
    let range = document.createRange();
    range.selectNodeContents(node);
    range.collapse(false);
    let sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}

// 删除 localStorage sessionStorage
export const removeStorage = (key) => {
    localStorage.removeItem(key)
    sessionStorage.removeItem(key)
}

// 删除 cookie
export const removeCookie = (name) => {
    document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
}

// 文件大小转换
export const fileSize = (size, isMb = false) => {
    if(size >= 1024 ** 3){
        return `${isMb ? (size / 1024 ** 3).toFixed(0) : (size / 1024 ** 3).toFixed(2)}GB`
    }else if(size >= 1024 ** 2){
        return `${isMb ? (size / 1024 ** 2).toFixed(0) : (size / 1024 ** 2).toFixed(2)}MB`
    }else if(size >= 1024){
        return `${isMb ? (size / 1024).toFixed(0) : (size / 1024).toFixed(2)}KB`
    }else{
        return `${size}B`
    }
}

// 获取文件扩展名
export const extname = (name) => {
    const i = name.lastIndexOf('.');
    if (i >= 0) {
        return name.substring(i).toLowerCase();
    }
    return '';
}